package com.example.shoppingmall.cotroller;

import com.example.shoppingmall.Utils.Page;
import com.example.shoppingmall.config.MyLog;
import com.example.shoppingmall.pojo.Advertisingcode;
import com.example.shoppingmall.pojo.Advertisingspace;
import com.example.shoppingmall.service.AdvertisingspaceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/Advertisingspace")
public class AdvertisingspaceController {
    @Autowired
    AdvertisingspaceService advertisingspaceService;

    @RequestMapping("/Advertisingspacelist")
    public String Advertisingspacelist(@RequestParam(value = "name",required = false) String name, @RequestParam(value = "utime",required = false) String utime,
                                       @RequestParam(value = "pageno",defaultValue = "1") String pageno, @RequestParam(value = "pageSize",defaultValue = "6") String pageSize,
                                       Model model){
        int count=advertisingspaceService.spacecount(name, utime);
        int num=(Integer.valueOf(pageno)-1)*Integer.valueOf(pageSize);
        Page page=new Page();
        page.setPageno(Integer.valueOf(pageno));
        page.setPageCount(count);
        List<Advertisingspace> advertisingspaceList=advertisingspaceService.getAdvertisingspacelist(name, utime, num, Integer.valueOf(pageSize));
        model.addAttribute("page",page);
        model.addAttribute("advertisingspaceList",advertisingspaceList);
        model.addAttribute("name",name);
        model.addAttribute("utime",utime);
        List<Advertisingcode> advertisingcodes=advertisingspaceService.getcodelist();
        model.addAttribute("advertisingcodes",advertisingcodes);
        return "Operate/Advertisingspace";
    }

    //添加广告位列表
    @MyLog(value = "运营管理->广告位->添加")
    @RequestMapping("/addspace")
    public String addspace(@RequestParam("aspace") String aspace,@RequestParam("aCode") String aCode,
                           @RequestParam("sort") String sort){
        Advertisingspace advertisingspace=new Advertisingspace();
        advertisingspace.setAspace(aspace);
        advertisingspace.setaCode(Integer.valueOf(aCode));
        advertisingspace.setSort(Integer.valueOf(sort));
        if(advertisingspaceService.addspace(advertisingspace)>0){
            return "redirect:/Advertisingspace/Advertisingspacelist";
        }
        return "error";
    }

    //删除
    @MyLog(value = "运营管理->广告位->删除")
    @RequestMapping("/delspace")
    @ResponseBody
    public Map<Object,Object> delspace(Integer id){
        Map<Object,Object> map=new HashMap<Object,Object>();
        if(advertisingspaceService.delspace(id)>0){
            map.put("delspace","yes");
        }else {
            map.put("delspace", "no");
        }
        return map;
    }

    //根据id查询详情信息
    @MyLog(value = "运营管理->广告位->查看详情")
    @RequestMapping("/selspace")
    public String selspace(Integer id,Model model){
        Advertisingspace advertisingspace=advertisingspaceService.selspace(id);
        model.addAttribute("advertisingspace",advertisingspace);
        List<Advertisingcode> advertisingcodes=advertisingspaceService.getcodelist();
        model.addAttribute("codes",advertisingcodes);
        return "Operate/Updspace";
    }


    //修改广告位列表
    @MyLog(value = "运营管理->广告位->修改 ")
    @RequestMapping("/updspace")
    public String updspace(@RequestParam("aspace")String aspace,@RequestParam("aCode")String aCode,
                           @RequestParam("sort") String sort,@RequestParam("id") String id){
        Advertisingspace space=new Advertisingspace();
        space.setAspace(aspace);
        space.setaCode(Integer.valueOf(aCode));
        space.setSort(Integer.valueOf(sort));
        space.setId(Integer.valueOf(id));
        if(advertisingspaceService.updspace(space)>0){
            return "redirect:/Advertisingspace/Advertisingspacelist";
        }
        return "error";
    }
}
